﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using MoMoTransferVN_WP.utils;
using System.Windows.Threading;

namespace MoMoTransferVN_WP.page
{
    public partial class SplashPage : PhoneApplicationPage
    {
        public SplashPage()
        {
            InitializeComponent();

            // timer interval specified as 1 second
            newTimer.Interval = TimeSpan.FromSeconds(1);
            // Sub-routine OnTimerTick will be called at every 1 second
            newTimer.Tick += OnTimerTick;
            // starting the timer
            newTimer.Start();
        }

        // creating timer instance
        DispatcherTimer newTimer = new DispatcherTimer();

        int totalCount = 0;
        void OnTimerTick(Object sender, EventArgs args)
        {
            // text box property is set to current system date.
            // ToString() converts the datetime value into text
            totalCount++;
            if (totalCount == 1)
            {
                newTimer.Stop();
                handleNextPage();
            }
        }

        private void handleNextPage()
        {
            bool mIsSetup = SettingUtils.readBool(Assets.KEY_USER_SETUP);
            if (mIsSetup)
            {
                bool mIsRegister = SettingUtils.readBool(Assets.KEY_USER_REGISTER);
                if (mIsRegister)
                {
                    bool mIsLogin = SettingUtils.readBool(Assets.KEY_USER_LOGIN);
                    if (mIsLogin)
                    {
                        NavigationService.Navigate(new Uri("/page/MainPage.xaml", UriKind.Relative));
                    }
                    else
                    {
                        NavigationService.Navigate(new Uri("/page/LoginPage.xaml", UriKind.Relative));
                    }
                }
                else
                {
                    NavigationService.Navigate(new Uri("/page/EnterPinPage.xaml", UriKind.Relative));
                }
            }
            else
            {
                NavigationService.Navigate(new Uri("/page/newSetupPage.xaml", UriKind.Relative));
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            //NavigationService.GoBack();
        }
    }
}